// import moment from "moment";

import base64 from "@/components/Marking/util/base64";

export function getTreeData(data, idLabel = 'id', pidLabel = 'pid', parentValue = '') {
  data = JSON.parse(JSON.stringify(data))
  let dataDic = {}
  data.map(item => dataDic[item[idLabel]] = item)
  data.map(item => {
    let parent = dataDic[item[pidLabel]]
    if (!parent) return
    if (!parent.children) parent.children = []
    // item.parent = parent
    parent.children.push(item)
    // setCount(parent)
  })
  let topList = data.filter(item => item[pidLabel] === parentValue)
  return topList
}

function setCount(node) {
  node.count || (node.count = 0)
  node.count++
  if (node.parent) setCount(node.parent)
}

// export function formatDate (date, format) {
//   return moment(date).format(format)
// }

export function getMonthDateList(date) {
  (date instanceof Date) || (date = new Date(date))
  let dateList = [];
  let dateYear = date.getFullYear()
  let dateMonth = date.getMonth()
  let firstDate = new Date(`${dateYear}-${dateMonth + 1}-1`);
  let dateDay = firstDate.getDay()
  for (let i = 1; i <= 42; i++) {
    let pDay = new Date(dateYear, dateMonth, -dateDay + i);
    dateList.push({
      date: pDay,
      dateStr: formatDate(pDay, 'YYYY-MM-DD')
    });
  }
  return dateList
}

import {v4 as uuidv4} from 'uuid'
import {addEventListener, removeEventListener} from "./eventManager";

export const uuid = uuidv4

export function openMask(move = () => ({}), up = () => ({}), parent = document.body) {
  // let mask = document.createElement('div')
  // mask.style.backgroundColor = 'rgba(0,0,0,0)'
  // mask.style.position = 'absolute'
  // mask.style.left = 0
  // mask.style.top = 0
  // mask.style.right = 0
  // mask.style.bottom = 0
  // parent.appendChild(mask)

  let $move = e => {
    move(e)
  }
  let $up = e => {
    removeEventListener('mouseup', $up)
    removeEventListener('mousemove', $move)
    // mask.remove()
    up(e)
  }
  addEventListener('mousemove', $move)
  addEventListener('mouseup', $up)
}

export function decodeBase64(svgBase64) {
  return base64.decode(svgBase64.replace(/data:\w+\/[^;]+;base64,/, ''))
}

export function copyText(text = '') {
  let tempEl = document.createElement('textarea')
  tempEl.value = text
  tempEl.display = 'none'
  document.body.appendChild(tempEl)
  tempEl.select()
  document.execCommand('copy')
  tempEl.parentElement.removeChild(tempEl)
}

export default {
  getTreeData,
  // formatDate,
  getMonthDateList,
  uuid,
  copyText,
  decodeBase64,
}
